package com.ac.scum.log.web.model.pack.service;

import com.ac.common.core.exception.BaseException;
import com.ac.scum.log.bean.Package;
import com.ac.scum.log.bean.Player;
import com.ac.scum.log.bean.PlayerPackage;
import com.ac.scum.log.web.model.pack.dto.PlayerPackageDTO;
import com.ac.scum.log.web.model.pack.mapper.PackageMapper;
import com.ac.scum.log.web.model.pack.mapper.PlayerPackageMapper;
import com.ac.scum.log.web.model.player.mapper.PlayerMapper;
import com.ac.scum.log.web.utils.user.CurrentUserUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @author : acfan
 * date : create in 2024/5/28 18:11
 * description :
 **/
@Slf4j
@Service
@AllArgsConstructor
public class ServerPackageService {
    private final PlayerPackageMapper playerPackageMapper;
    private final PackageMapper packageMapper;
    private final PlayerMapper playerMapper;

    public Page<PlayerPackageDTO> page(Long current, Long size) throws BaseException {
        Page<PlayerPackage> playerPackagePage = playerPackageMapper.selectPage(new Page<>(current, size), Wrappers.lambdaQuery(PlayerPackage.class).eq(PlayerPackage::getServerId, CurrentUserUtil.getCurrentServerId()));
        Page<PlayerPackageDTO> resultPage = new Page<>();
        resultPage.setCurrent(playerPackagePage.getCurrent());
        resultPage.setPages(playerPackagePage.getPages());
        resultPage.setTotal(playerPackagePage.getTotal());
        List<PlayerPackage> records = playerPackagePage.getRecords();
        List<PlayerPackageDTO> packageLists = Lists.newArrayList();
        List<Long> packIds = Lists.newArrayList();
        List<Long> playerSteamIds = Lists.newArrayList();
        for (PlayerPackage record : records) {
            packIds.add(record.getPackageId());
            playerSteamIds.add(record.getSteamId());
        }


        Map<Long, Player> playerMap = Maps.newHashMap();
        try {
            playerMap = playerMapper.findPlayerByServerIdAndSteamIds(CurrentUserUtil.getCurrentServerId(), playerSteamIds).stream().collect(Collectors.toMap(Player::getSteamId, Function.identity(), (existingValue, newValue) -> newValue));
        } catch (Exception e) {
            log.error("获取玩家信息失败", e);
        }
        Map<Long, Package> packageMap = Maps.newHashMap();
        try {
            if (!packIds.isEmpty()) {
                packageMap = packageMapper.selectList(Wrappers.lambdaQuery(Package.class).in(Package::getId, packIds)).stream().collect(Collectors.toMap(Package::getId, Function.identity()));
            }
        } catch (Exception e) {
            log.error("获取套餐信息失败", e);
        }

        for (PlayerPackage record : records) {
            PlayerPackageDTO playerPackageDTO = new PlayerPackageDTO();
            playerPackageDTO.setExpireTime(record.getExpirationTime());
            playerPackageDTO.setPack(packageMap.get(record.getPackageId()));
            playerPackageDTO.setPlayer(playerMap.get(record.getSteamId()));
            packageLists.add(playerPackageDTO);
        }

        resultPage.setRecords(packageLists);
        return resultPage;
    }

    public List<Package> findByServerId(Long id) {
        return packageMapper.selectByServerId(id);
    }
}
